Page({
  data: {
      openId: '',
      email: '',
      password:''
  },

  onLoad: function(options) {
      this.setData({
          openId: options.openId
      });
  },

  onEmailInput: function(e) {
      this.setData({
          email: e.detail.value
      });
  },

  onPhoneInput: function(e) {
      this.setData({
          password: e.detail.value
      });
  },

  validateEmail: function(email) {
      const emailPattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
      return emailPattern.test(email);
  },


  onBind: function() {
      const { openId, email, password } = this.data;

      if (!email || !password) {
          wx.showToast({
              title: '请输入完整信息',
              icon: 'none'
          });
          return;
      }

      if (!this.validateEmail(email)) {
          wx.showToast({
              title: '请输入有效的邮箱地址',
              icon: 'none'
          });
          return;
      }

      wx.request({
          url: 'http://localhost:8080/gpt/bind',
          method: 'POST',
          data: {
              openId: openId,
              email: email,
              password:password
          },
          success: function(response) {
              wx.showToast({
                  title: '绑定成功',
                  icon: 'success'
              });
              wx.navigateBack();
              wx.request({
                url: 'http://localhost:8080/gpt/login',
                method:'post',
                data:{
                  email:email,
                  password:password
                }
              })
          },
          fail: function(error) {
              wx.showToast({
                  title: '绑定失败，请稍后再试',
                  icon: 'none'
              });
          }
      });
  }
});
